﻿/******************************************************************
** 文件名:T_005NovelInfoDAL.cs
** Copyright (c) 
** 创建人:
** 日 期:2012-11-15 11:06:26
** 描 述:数据操作类
** 版 本:v1.0
********************************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Novel.Entity;
using Novel.DAL.Base;

namespace Novel.DAL
{
    /// <summary>
    /// 数据操作类：T_005NovelInfo_DAL
    /// </summary>
    public class T_005NovelInfo_DAL : IT_005NovelInfoManager
    {


        #region 表名
        public  string GetTableName()
        {
            return "T_005NovelInfo";
        }
        #endregion
        #region 插入记录
        /// <summary>
        /// 插入记录
        /// </summary>
        /// <returns></returns>
        public int Insert(T_005NovelInfoEntity model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.AppendFormat("INSERT INTO {0}",GetTableName());
			strSql.Append("(BookID,ChaptersCount,LastUpdateTime,LastChapter,LastTaskID,LastChapterUrl,CmfuChapterCount,Hits,MonthHits,WeekHits,Recommend,MonthRecommend,WeekRecommend,Length,UpdateStatus)");
			strSql.Append(" values ");
			strSql.Append("(@BookID,@ChaptersCount,@LastUpdateTime,@LastChapter,@LastTaskID,@LastChapterUrl,@CmfuChapterCount,@Hits,@MonthHits,@WeekHits,@Recommend,@MonthRecommend,@WeekRecommend,@Length,@UpdateStatus)");
			strSql.Append(" SELECT @ = @@IDENTITY");
			SqlCommand cmd = new SqlCommand(strSql.ToString());
			cmd.Parameters.Add("@InfoID",SqlDbType.Int);
			cmd.Parameters["@InfoID"].Direction = ParameterDirection.Output;
		
			cmd.Parameters.Add("@BookID",SqlDbType.Int,4);
			cmd.Parameters["@BookID"].Value = model.BookID;
			cmd.Parameters.Add("@ChaptersCount",SqlDbType.Int,4);
			cmd.Parameters["@ChaptersCount"].Value = model.ChaptersCount;
			cmd.Parameters.Add("@LastUpdateTime",SqlDbType.DateTime);
			cmd.Parameters["@LastUpdateTime"].Value = model.LastUpdateTime;
			cmd.Parameters.Add("@LastChapter",SqlDbType.VarChar,100);
			cmd.Parameters["@LastChapter"].Value = model.LastChapter;
			cmd.Parameters.Add("@LastTaskID",SqlDbType.Int,4);
			cmd.Parameters["@LastTaskID"].Value = model.LastTaskID;
			cmd.Parameters.Add("@LastChapterUrl",SqlDbType.VarChar,50);
			cmd.Parameters["@LastChapterUrl"].Value = model.LastChapterUrl;
			cmd.Parameters.Add("@CmfuChapterCount",SqlDbType.Int,4);
			cmd.Parameters["@CmfuChapterCount"].Value = model.CmfuChapterCount;
			cmd.Parameters.Add("@Hits",SqlDbType.Int,4);
			cmd.Parameters["@Hits"].Value = model.Hits;
			cmd.Parameters.Add("@MonthHits",SqlDbType.Int,4);
			cmd.Parameters["@MonthHits"].Value = model.MonthHits;
			cmd.Parameters.Add("@WeekHits",SqlDbType.Int,4);
			cmd.Parameters["@WeekHits"].Value = model.WeekHits;
			cmd.Parameters.Add("@Recommend",SqlDbType.Int,4);
			cmd.Parameters["@Recommend"].Value = model.Recommend;
			cmd.Parameters.Add("@MonthRecommend",SqlDbType.Int,4);
			cmd.Parameters["@MonthRecommend"].Value = model.MonthRecommend;
			cmd.Parameters.Add("@WeekRecommend",SqlDbType.Int,4);
			cmd.Parameters["@WeekRecommend"].Value = model.WeekRecommend;
			cmd.Parameters.Add("@Length",SqlDbType.Int,4);
			cmd.Parameters["@Length"].Value = model.Length;
			cmd.Parameters.Add("@UpdateStatus",SqlDbType.TinyInt,1);
			cmd.Parameters["@UpdateStatus"].Value = model.UpdateStatus;
			SQLHelper da = new SQLHelper();
			int EffectRows = da.ExecuteNonQuery(cmd);
            model.InfoID = (int)cmd.Parameters["@InfoID"].Value;
			return EffectRows;
		}
        #endregion

        #region 更新数据
        /// <summary>
        /// 更新数据
        /// </summary>
        /// <returns></returns>
        public int Update(T_005NovelInfoEntity model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.AppendFormat("UPDATE {0} SET ", GetTableName());
            strSql.Append("BookID=@BookID,ChaptersCount=@ChaptersCount,LastUpdateTime=@LastUpdateTime,LastChapter=@LastChapter,LastTaskID=@LastTaskID,LastChapterUrl=@LastChapterUrl,CmfuChapterCount=@CmfuChapterCount,Hits=@Hits,MonthHits=@MonthHits,WeekHits=@WeekHits,Recommend=@Recommend,MonthRecommend=@MonthRecommend,WeekRecommend=@WeekRecommend,Length=@Length,UpdateStatus=@UpdateStatus");
            strSql.Append(" WHERE InfoID=@InfoID");
            SqlCommand cmd = new SqlCommand(strSql.ToString());
            cmd.Parameters.Add("@InfoID", SqlDbType.Int, 4);
            cmd.Parameters["@InfoID"].Value = model.InfoID;
            cmd.Parameters.Add("@BookID", SqlDbType.Int, 4);
            cmd.Parameters["@BookID"].Value = model.BookID;
            cmd.Parameters.Add("@ChaptersCount", SqlDbType.Int, 4);
            cmd.Parameters["@ChaptersCount"].Value = model.ChaptersCount;
            cmd.Parameters.Add("@LastUpdateTime", SqlDbType.DateTime);
            cmd.Parameters["@LastUpdateTime"].Value = model.LastUpdateTime;
            cmd.Parameters.Add("@LastChapter", SqlDbType.VarChar, 100);
            cmd.Parameters["@LastChapter"].Value = model.LastChapter;
            cmd.Parameters.Add("@LastTaskID", SqlDbType.Int, 4);
            cmd.Parameters["@LastTaskID"].Value = model.LastTaskID;
            cmd.Parameters.Add("@LastChapterUrl", SqlDbType.VarChar, 50);
            cmd.Parameters["@LastChapterUrl"].Value = model.LastChapterUrl;
            cmd.Parameters.Add("@CmfuChapterCount", SqlDbType.Int, 4);
            cmd.Parameters["@CmfuChapterCount"].Value = model.CmfuChapterCount;
            cmd.Parameters.Add("@Hits", SqlDbType.Int, 4);
            cmd.Parameters["@Hits"].Value = model.Hits;
            cmd.Parameters.Add("@MonthHits", SqlDbType.Int, 4);
            cmd.Parameters["@MonthHits"].Value = model.MonthHits;
            cmd.Parameters.Add("@WeekHits", SqlDbType.Int, 4);
            cmd.Parameters["@WeekHits"].Value = model.WeekHits;
            cmd.Parameters.Add("@Recommend", SqlDbType.Int, 4);
            cmd.Parameters["@Recommend"].Value = model.Recommend;
            cmd.Parameters.Add("@MonthRecommend", SqlDbType.Int, 4);
            cmd.Parameters["@MonthRecommend"].Value = model.MonthRecommend;
            cmd.Parameters.Add("@WeekRecommend", SqlDbType.Int, 4);
            cmd.Parameters["@WeekRecommend"].Value = model.WeekRecommend;
            cmd.Parameters.Add("@Length", SqlDbType.Int, 4);
            cmd.Parameters["@Length"].Value = model.Length;
            cmd.Parameters.Add("@UpdateStatus", SqlDbType.TinyInt, 1);
            cmd.Parameters["@UpdateStatus"].Value = model.UpdateStatus;
            SQLHelper da = new SQLHelper();
            return da.ExecuteNonQuery(cmd);
        }
        /// <summary>
        /// 更新点击数、推荐数
        /// </summary>
        /// <returns></returns>
        public int UpdateHitCount(T_005NovelInfoEntity model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.AppendFormat("UPDATE {0} SET ", GetTableName());
            strSql.Append("CmfuChapterCount=@CmfuChapterCount,Hits=@Hits,MonthHits=@MonthHits,WeekHits=@WeekHits,Recommend=@Recommend,MonthRecommend=@MonthRecommend,WeekRecommend=@WeekRecommend,Length=@Length");
            strSql.Append(" WHERE BookID=@BookID; UPDATE T_001GatherTaskLog SET CmfuChapterCount=@CmfuChapterCount WHERE BookID=@BookID");
            SqlCommand cmd = new SqlCommand(strSql.ToString());
          
            cmd.Parameters.Add("@BookID", SqlDbType.Int, 4);
            cmd.Parameters["@BookID"].Value = model.BookID;
          
            cmd.Parameters.Add("@CmfuChapterCount", SqlDbType.Int, 4);
            cmd.Parameters["@CmfuChapterCount"].Value = model.CmfuChapterCount;
            cmd.Parameters.Add("@Hits", SqlDbType.Int, 4);
            cmd.Parameters["@Hits"].Value = model.Hits;
            cmd.Parameters.Add("@MonthHits", SqlDbType.Int, 4);
            cmd.Parameters["@MonthHits"].Value = model.MonthHits;
            cmd.Parameters.Add("@WeekHits", SqlDbType.Int, 4);
            cmd.Parameters["@WeekHits"].Value = model.WeekHits;
            cmd.Parameters.Add("@Recommend", SqlDbType.Int, 4);
            cmd.Parameters["@Recommend"].Value = model.Recommend;
            cmd.Parameters.Add("@MonthRecommend", SqlDbType.Int, 4);
            cmd.Parameters["@MonthRecommend"].Value = model.MonthRecommend;
            cmd.Parameters.Add("@WeekRecommend", SqlDbType.Int, 4);
            cmd.Parameters["@WeekRecommend"].Value = model.WeekRecommend;
            cmd.Parameters.Add("@Length", SqlDbType.Int, 4);
            cmd.Parameters["@Length"].Value = model.Length;
          
            SQLHelper da = new SQLHelper();
            return da.ExecuteNonQuery(cmd);
        }
        /// <summary>
        /// 更新章节数
        /// </summary>
        /// <returns></returns>
        public int UpdateChapterCount(int bookID, int ChapterCount)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.AppendFormat("UPDATE {0} SET ", GetTableName());
            strSql.Append("CmfuChapterCount=@CmfuChapterCount");
            strSql.Append(" WHERE BookID=@BookID; UPDATE T_001GatherTaskLog SET CmfuChapterCount=@CmfuChapterCount WHERE BookID=@BookID");
            SqlCommand cmd = new SqlCommand(strSql.ToString());

            cmd.Parameters.Add("@BookID", SqlDbType.Int, 4);
            cmd.Parameters["@BookID"].Value = bookID;

            cmd.Parameters.Add("@CmfuChapterCount", SqlDbType.Int, 4);
            cmd.Parameters["@CmfuChapterCount"].Value = ChapterCount;
         

            SQLHelper da = new SQLHelper();
            return da.ExecuteNonQuery(cmd);
        }
        #endregion

        #region 删除数据
        /// <summary>
        /// 删除数据
        /// </summary>
        /// <returns></returns>
        public int Delete(int id)
        {
            string sql = string.Format("DELETE {0} WHERE InfoID=@InfoID", GetTableName());
            SqlCommand command = new SqlCommand(sql);
            command.Parameters.Add("@InfoID", SqlDbType.Int, 4);
            command.Parameters["@InfoID"].Value = id;
            SQLHelper da = new SQLHelper();
            return da.ExecuteNonQuery(command);
        }
        #endregion

        #region IEntityFacade 成员
        /// <summary>
        /// 填充数据
        /// </summary>
        /// <param name="reader"></param>
        public EntityBase FillDetailWithReader(IDataReader reader)
        {
            T_005NovelInfoEntity model = new T_005NovelInfoEntity();
            if (reader["InfoID"] != DBNull.Value)
                model.InfoID = (int)reader["InfoID"];
            if (reader["BookID"] != DBNull.Value)
                model.BookID = (int)reader["BookID"];
            if (reader["ChaptersCount"] != DBNull.Value)
                model.ChaptersCount = (int)reader["ChaptersCount"];
            if (reader["LastUpdateTime"] != DBNull.Value)
                model.LastUpdateTime = (DateTime)reader["LastUpdateTime"];
            if (reader["LastChapter"] != DBNull.Value)
                model.LastChapter = (string)reader["LastChapter"];
            if (reader["LastTaskID"] != DBNull.Value)
                model.LastTaskID = (int)reader["LastTaskID"];
            if (reader["LastChapterUrl"] != DBNull.Value)
                model.LastChapterUrl = (string)reader["LastChapterUrl"];
            if (reader["CmfuChapterCount"] != DBNull.Value)
                model.CmfuChapterCount = (int)reader["CmfuChapterCount"];
            if (reader["Hits"] != DBNull.Value)
                model.Hits = (int)reader["Hits"];
            if (reader["MonthHits"] != DBNull.Value)
                model.MonthHits = (int)reader["MonthHits"];
            if (reader["WeekHits"] != DBNull.Value)
                model.WeekHits = (int)reader["WeekHits"];
            if (reader["Recommend"] != DBNull.Value)
                model.Recommend = (int)reader["Recommend"];
            if (reader["MonthRecommend"] != DBNull.Value)
                model.MonthRecommend = (int)reader["MonthRecommend"];
            if (reader["WeekRecommend"] != DBNull.Value)
                model.WeekRecommend = (int)reader["WeekRecommend"];
            if (reader["Length"] != DBNull.Value)
                model.Length = (int)reader["Length"];
            if (reader["UpdateStatus"] != DBNull.Value)
                model.UpdateStatus = (T_005NovelInfoEntity.EnumUpdateStatus)(byte)reader["UpdateStatus"];
            return model;
        }
        #endregion

        public T_005NovelInfoEntity GetObjectByID(int ID)
        {
            SQLHelper da = new SQLHelper();
            string strSql = string.Format("select * {0} WHERE BookID=@BookID", GetTableName());
            SQLHelper.MappEntity<T_005NovelInfoEntity> dele_Entity = delegate(SqlDataReader reader)
            {
                return (T_005NovelInfoEntity)FillDetailWithReader(reader);
            };
            return da.Exceute<T_005NovelInfoEntity>(new SqlCommand(strSql), dele_Entity);

        }


        #region 数据集
        public DataTable ShowTable()
        {

            string strSql = string.Format("SELECT  * FROM {0}", GetTableName());
            return SQLHelper.Execute(strSql.ToString());
        }
        #endregion

        #region 分页数据集
        public DataTable ShowTable(PageEntity entity, out int totalPages, out int totalCount)
        {
            QueryBase manager = new QueryBase();

            entity.tbName = GetTableName();
            DataTable dt = manager.GetTable(entity);
            totalPages = entity.TotalPages;
            totalCount = entity.TotalCount;
            return dt;
        }
        #endregion
    }
}
